﻿using MT.CustomUserControl.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;

namespace MT.CustomUserControl.Views
{
    /// <summary>
    /// SearchUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class SearchUserControl : UserControl
    {
        private SearchUcViewModel searchUcViewModel;

        public SearchUserControl()
        {
            InitializeComponent();

            searchUcViewModel = DataContext as SearchUcViewModel;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if(DataContext != null)
                searchUcViewModel = DataContext as SearchUcViewModel;
        }

        public void ClearDatePicker()
        {
            datePicker_Start.SelectedDate = null;
            datePicker_End.SelectedDate = null;

            searchUcViewModel.StartDate = string.Empty;
            searchUcViewModel.EndDate = string.Empty;
        }

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton != null)
            {
                searchUcViewModel.Gender = (string)radioButton.Content;
            }
        }

        private void datePicker_Start_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (datePicker_Start.SelectedDate != null)
            {
                if (datePicker_End.SelectedDate != null)
                {
                    if (DateTime.Compare((DateTime)datePicker_Start.SelectedDate, (DateTime)datePicker_End.SelectedDate) > 0)
                    {
                        datePicker_Start.SelectedDate = datePicker_End.SelectedDate;
                        searchUcViewModel.EndDate = ((DateTime)datePicker_End.SelectedDate).ToString("yyyy-MM-dd");
                        MessageBox.Show("起始日期不能大于终止日期！", "提示");
                        return;
                    }
                }

                searchUcViewModel.StartDate = ((DateTime)datePicker_Start.SelectedDate).ToString("yyyy-MM-dd");
            }               
        }

        private void datePicker_End_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (datePicker_End.SelectedDate != null)
            {
                if (datePicker_Start.SelectedDate != null)
                {
                    if (DateTime.Compare((DateTime)datePicker_Start.SelectedDate, (DateTime)datePicker_End.SelectedDate) > 0)
                    {
                        datePicker_End.SelectedDate = datePicker_Start.SelectedDate;
                        searchUcViewModel.EndDate = ((DateTime)datePicker_End.SelectedDate).ToString("yyyy-MM-dd");
                        MessageBox.Show("终止日期不能小于起始日期！", "提示");
                        return;
                    }
                }

                searchUcViewModel.EndDate = ((DateTime)datePicker_End.SelectedDate).ToString("yyyy-MM-dd");
            }
        }

        private void Btn_Reset_Click(object sender, RoutedEventArgs e)
        {
            ClearDatePicker();
            searchUcViewModel.BarCode = string.Empty;
            searchUcViewModel.Name = string.Empty;
            searchUcViewModel.Gender = string.Empty;
            searchUcViewModel.Phone = string.Empty;
            RadioButton_Man.IsChecked = false;
            RadioButton_WoMan.IsChecked = false;
        }
    }
}
